//
// Created by Administrator on 2021/10/20.
//
#include "function2.h"
#include "iostream"
#include "list"
#include "algorithm"
#include "iterator"

using namespace std;

void workingWithLists(list<int>& firstList, list<int>& secondList, int value1, int value2, int value3)
{
    ostream_iterator<int> screen(cout, " ");

    cout << "First list: ";
    copy(firstList.begin(), firstList.end(), screen);

    cout << "\nSearch " << value1 << ": ";
    // use std::find on firstList
    auto it = find(firstList.begin(), firstList.end(), value1);
    cout << (it != firstList.end() ? "Found" : "Not found");

    cout << "\nReplace" << value2 << " with " << value3 << ": ";
    // use std::replace on firstList.
    replace(firstList.begin(), firstList.end(), value2, value3);

    copy(firstList.begin(), firstList.end(), screen);
    cout << "\nDelete " << value3 << ": ";
    // use std::find and list::erase on firstList
    it = find(firstList.begin(), firstList.end(), value3);
    if (it != firstList.end()) {
        firstList.erase(it);
        it = find(firstList.begin(), firstList.end(), value3);
    }

    copy(firstList.begin(), firstList.end(), screen);
    cout << "\nSort the list: ";
    // use list::sort on firstList
    firstList.sort();

    copy(firstList.begin(), firstList.end(), screen);
    cout << "\nReverse the list starting from the 3rd element: ";
    // use std::reverse on firstList
    it = firstList.begin();
    advance(it, 2);
    reverse(it, firstList.end());

    copy(firstList.begin(), firstList.end(), screen);
    cout << "\nSecond list: ";
    copy(secondList.begin(), secondList.end(), screen);
    cout << "\nMove last three elements of the second list " << "into the first list at position fourth.\n";
    // use list::splice
    it = secondList.end();
    advance(it, -3);
    firstList.splice(++++++firstList.begin(), secondList, it, secondList.end());

    cout << "First list: ";
    copy(firstList.begin(), firstList.end(), screen);
    cout << "\nSecond list: ";
    copy(secondList.begin(), secondList.end(), screen);
    cout << endl;
}

